/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.blockly.ohos.ui;

import com.google.blockly.ohos.ResourceTable;
import com.google.blockly.utils.ImageUtils;
import com.google.blockly.utils.LogUtils;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
 * Drop target view for deleting blocks via a drag gesture, with animation.
 * <p/>
 * This view has two layout attributes, {@code closedIcon} and {@code openedIcon}. Each is a
 * reference to a drawable resource for one of the two trash states, closed (default/idle state) and
 * opened (pending drop during drag).  The TrashCanView assumes both drawables are the same size, so
 * the overall view will not change size when the state changes.
 */
public class TrashCanView extends Image {
    private static final String TAG = "TrashCanView";

    @Retention(SOURCE)
    public @interface HoverState {
    }

    public static final int STATE_DEFAULT = 0;
    public static final int STATE_ON_HOVER = 1;

    public static final int TRASH_SCOPE = 100;

    protected int mState = STATE_DEFAULT;
    protected PixelMap mDefaultDrawable;
    protected PixelMap mOnHoverDrawable;

    public TrashCanView(Context context) {
        super(context);
    }

    public TrashCanView(Context context, AttrSet attrs) {
        super(context, attrs);
        if (attrs.getAttr("defaultIcon").isPresent()) {
            PixelMapElement element = (PixelMapElement) attrs.getAttr("defaultIcon").get().getElement();
            PixelMap pixelMap = element.getPixelMap();
            setDefaultIcon(pixelMap);
        } else {
            setDefaultIcon(ResourceTable.Media_blockly_trash);
        }

        if (attrs.getAttr("onHoverIcon").isPresent()) {
            PixelMapElement element = (PixelMapElement) attrs.getAttr("onHoverIcon").get().getElement();
            PixelMap pixelMap = element.getPixelMap();
            setOnHoverIcon(pixelMap);
        } else {
            setOnHoverIcon(ResourceTable.Media_blockly_trash_open);
        }
    }

    /**
     * Assigns a drawable resource id for the default state, when no block is hovering with a
     * pending drop. Usually, this is a closed trashcan.
     *
     * @param drawableRes Default drawable resource id.
     */
    public void setDefaultIcon(int drawableRes) {
        setDefaultIcon(ImageUtils.getPixMap(getContext(), drawableRes));
    }

    /**
     * Assigns a drawable resource if for the default state, when no block is hovering with a
     * pending drop. Usually, this is a closed trashcan.
     *
     * @param drawable Default drawable.
     */
    public void setDefaultIcon(PixelMap drawable) {
        mDefaultDrawable = drawable;
        if (mState == STATE_DEFAULT) {
            setPixelMap(mDefaultDrawable);
        }
    }

    /**
     * Assigns a drawable resource id for the hovered state, when a block has been dragged above
     * this view. Usually, this is a open trashcan.
     *
     * @param drawableRes Drawable resource id for the hovered
     */
    public void setOnHoverIcon(int drawableRes) {
        setOnHoverIcon(ImageUtils.getPixMap(getContext(), drawableRes));
    }

    /**
     * Assigns a drawable for the hovered state, when a block has been dragged above this view.
     * Usually, this is a open trashcan.
     *
     * @param drawable
     */
    public void setOnHoverIcon(PixelMap drawable) {
        mOnHoverDrawable = drawable;
        if (mState == STATE_ON_HOVER) {
            setPixelMap(mOnHoverDrawable);
        }
    }

    public void setState(@HoverState int state) {
        mState = state;
        switch (state) {
            default:
                LogUtils.w(TAG, "Invalid state: " + state);
                // continue to default
            case STATE_DEFAULT:
                setPixelMap(mDefaultDrawable);
                break;
            case STATE_ON_HOVER:
                setPixelMap(mOnHoverDrawable);
                break;
        }
    }
}
